package com.superitem.school.controller;

import com.superitem.school.pojo.Student;
import com.superitem.school.service.StudentService;
import com.superitem.school.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@CrossOrigin
//@RestController
@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    public StudentService studentService;

    @RequestMapping("tostuupdate")
    public String StuUpdate(Model model){
        Student student = studentService.selectById(2);
        model.addAttribute("student", student);
        System.out.println(student);
        return "student/stuupdate";
    }

    @RequestMapping("stuupdate")
    public String StuShow(Student student, Model model){
        studentService.updateStu(student);
        model.addAttribute("student",studentService.selectById(student.getStuId()));
        return "student/stushow";
    }

//    @RequestMapping("/show_all_student")
//    public JsonResult ShowAllStudent() {
//        List<Student> students = studentService.showAllStudent();
//        return new JsonResult(students);
//
//    }

    @RequestMapping("/show_all_student")
    public String ShowAllStudentByCon(Model model) {
        List<Student> students = studentService.showAllStudent();
        model.addAttribute("students", students);
        System.out.println(students);
        return "student/stushow";

    }

    @RequestMapping("/select/{id}")
    public JsonResult SelectById(@PathVariable("id")Integer id){
        return new JsonResult(studentService.selectById(id));
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
